package com.dabbo.starship.app.client;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import com.dabbo.starship.interfaces.Cell;

public class RoomViewUI extends JPanel {
	
	private static final long serialVersionUID = 5635078709560388201L;

	public static int ySize = 55;
	public static int xSize = 55;

	private Cell[][] grid = new Cell[9][9];
	
	private BufferedImage 	imageFloor;
	private BufferedImage 	imageWall;
	private BufferedImage 	imageBattery;
	private BufferedImage 	imageConsole;
	private BufferedImage 	imageDoor;
	private BufferedImage 	imageEngine;
	private BufferedImage 	imageEngineCore;
	private BufferedImage 	imageReactorOff;
	private BufferedImage 	imageReactorOn;
	private BufferedImage	imageCargoOffline;
	private BufferedImage	imageCargoOnline;
	private BufferedImage 	imageNoLight;
	private BufferedImage 	imageUnit;
	private BufferedImage 	imageCargo;	
	
	// To be replaced with an array of unit objects.
	private int 			playerY = 0;
	private int				playerX = 0;
	
	private boolean 		isRoomLit	= false;
			
	public RoomViewUI(Cell[][] grid) {
		this.grid = grid;
		try {
			imageFloor = ImageIO.read(new File("img/Tile_Floor.png"));
			imageWall = ImageIO.read(new File("img/Tile_Wall.png"));
			imageBattery = ImageIO.read(new File("img/Tile_Battery.png"));
			imageConsole = ImageIO.read(new File("img/Tile_Console.png"));
			imageDoor = ImageIO.read(new File("img/Tile_Door.png"));
			imageEngine = ImageIO.read(new File("img/Tile_Engine.png"));
			imageEngineCore = ImageIO.read(new File("img/Tile_EngineCore.png"));
			imageReactorOff = ImageIO.read(new File("img/Tile_PowerOff.png"));
			imageReactorOn = ImageIO.read(new File("img/Tile_PowerOn.png"));
			imageCargoOffline = ImageIO.read(new File("img/Tile_CargoOffline.png"));
			imageCargoOnline = ImageIO.read(new File("img/Tile_CargoOnline.png"));
			imageUnit = ImageIO.read(new File("img/Unit.png"));
			imageCargo = ImageIO.read(new File("img/Item.png"));
			imageNoLight = ImageIO.read(new File("img/Tile_NoLight.png"));
		} catch (IOException ex) {
			System.out.println("Argh, Image failure.");
		}
	}
	
	public void setPlayerPos(int cy, int cx) {
		this.playerY = cy;
		this.playerX = cx;
	}
	
	public void setRoomLights(boolean isRoomLit) {
		this.isRoomLit = isRoomLit;
	}
	
	public boolean isOpaque() {
		return true;
	}
	
	public void setGrid(Cell[][] grid) {
		this.grid = grid;
	}
	
	protected void paintComponent(Graphics g) {
        for (int y = 0; y < 9; y++) {
        	for (int x = 0; x < 9; x++) {
        		BufferedImage imageTemp = imageNoLight;
        		if (grid[y][x] != null) {
        			try {
        				switch (grid[y][x].getCellType()) {
        				case Cell.CELL_DEFAULT:
        					imageTemp = imageWall;
        					break;
        				case Cell.CELL_FLOOR:
        					imageTemp = imageFloor;
        					break;
        				case Cell.CELL_BATTERY:
        					imageTemp = imageBattery;
        					break;
        				case Cell.CELL_CONSOLE:
        					imageTemp = imageConsole;
        					break;
        				case Cell.CELL_DOOR:
        					imageTemp = imageDoor;
        					break;
        				case Cell.CELL_ENGINE:
        					imageTemp = imageEngine;
        					break;
        				case Cell.CELL_ENGINE_CORE:
        					imageTemp = imageEngineCore;
        					break;
        				case Cell.CELL_POWER:
        					imageTemp = imageReactorOff;
        					break;
        				case Cell.CELL_CARGO:
        					// Only show fully powered floor tiles
        					if (grid[y][x].getPowerOutput() == -7) {
        						imageTemp = imageCargoOnline;
        					} else if (grid[y][x].getPowerOutput() <= -6) {
        						if ((Math.random() * 5) > 2) {
        							imageTemp = imageCargoOnline;
        						} else {
        							imageTemp = imageCargoOffline;
        						}
        					} else {
        						imageTemp = imageCargoOffline;
        					}
        					break;
        				default:
        					imageTemp = imageWall;
        					System.out.println("Unknown cell type of : " + grid[y][x].getCellType());
        				}
        				if (!isRoomLit) {
        					if ((y < (playerY - 1)) || (y > (playerY + 1))) {
        						imageTemp = imageNoLight;
        					}
    						if ((x < (playerX - 1)) || (x > (playerX + 1))) {
    							imageTemp = imageNoLight;
    						}
        				}

        			} catch (RemoteException ex) {
        				System.out.println("RoomViewUI :: RemoteException on Image Draw");
        			}
        		}
				g.drawImage(imageTemp, (x * xSize), (y * ySize), (x * xSize) + xSize, (y * ySize) + ySize, 0, 0, imageTemp.getWidth(), imageTemp.getHeight(), null);
				try {
					if (grid[y][x].isCargoHere() && imageTemp != imageNoLight) {
				        g.drawImage(imageCargo, (x * xSize), (y * ySize), (x * xSize) + xSize, (y * ySize) + ySize, 0, 0, imageCargo.getWidth(), imageCargo.getHeight(), null);
					}					
				} catch (RemoteException ex) {
    				System.out.println("RoomViewUI :: RemoteException on Image Draw, Cargo check.");					
				}
        	}
        	
        }
        g.drawImage(imageUnit, (playerX * xSize), (playerY * ySize), (playerX * xSize) + xSize, (playerY * ySize) + ySize, 0, 0, imageUnit.getWidth(), imageUnit.getHeight(), null);
	}
	
}
